package com.dinggou.controller;

import com.dinggou.common.ResponseCode;
import com.dinggou.common.ServerResponse;
import com.dinggou.entity.Levels;
import com.dinggou.service.LevelsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/levels")
public class LevelsController {

    @Resource(name = "levelsServiceImpl")
    @Autowired
    private LevelsService levelsService;


    @RequestMapping("/delete")
    @ResponseBody
    public ServerResponse deleteByPrimaryKey(Integer id){
        if(levelsService.deleteByPrimaryKey(id)>0){
            return ServerResponse.createBySuccess(1,"删除管部门单位成功");
        }else{
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"删除数据失败");
        }
    }

    @RequestMapping("/insert")
    @ResponseBody
    public ServerResponse insert(Levels record){

        Levels levels = new Levels();
        levels.setName(record.getName());

        if(levelsService.insert(levels)>0){
            return ServerResponse.createBySuccess("添加数据成功",levels);
        }else{
            return ServerResponse.createByError();
        }
    }

    @RequestMapping("/insertSelective")
    @ResponseBody
    public ServerResponse insertSelective(Levels record){

        Levels levels = new Levels();
        levels.setName(record.getName());

        if(levelsService.insert(levels)>0){
            return ServerResponse.createBySuccess("添加数据成功",levels);
        }else{
            return ServerResponse.createByError();
        }
    }

    @RequestMapping("/selectByPrimaryKey")
    @ResponseBody
    public ServerResponse selectByPrimaryKey(Integer id, HttpServletRequest request, HttpServletResponse response) throws IOException {

        Levels levels = levelsService.selectByPrimaryKey(id);

        if(levels != null){
            return ServerResponse.createBySuccess(1,levels);
        }else{
            return ServerResponse.createByErrorMessage("找不到管理员");
        }
    }

    @RequestMapping("/listall")
    @ResponseBody
    public ServerResponse listall(HttpServletRequest request, HttpServletResponse response){
        List<Levels> levelsList=levelsService.selectAll();
        if(levelsList.size()>0){
            return ServerResponse.createBySuccess(levelsList.size(),levelsList);
        }else{
            return ServerResponse.createByErrorMessage("找不到管理员信息");
        }
    }

    @RequestMapping("/updateByPrimaryKeySelective")
    @ResponseBody
    public ServerResponse updateByPrimaryKeySelective(Levels record) {
        Levels levels = new Levels();
        levels.setId(record.getId());
        levels.setName(record.getName());

        if(levelsService.updateByPrimaryKeySelective(levels)>0){
            return ServerResponse.createBySuccess("更新数据成功",levels);
        }else{
            return ServerResponse.createByErrorMessage("更新数据失败");
        }

    }

    @RequestMapping("/updateByPrimaryKey")
    @ResponseBody
    public ServerResponse updateByPrimaryKey(Levels record){

        Levels levels = new Levels();
        levels.setId(record.getId());
        levels.setName(record.getName());

        if(levelsService.updateByPrimaryKeySelective(levels)>0){
            return ServerResponse.createBySuccess("更新数据成功",levels);
        }else{
            return ServerResponse.createByErrorMessage("更新数据失败");
        }

    }
}